﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Utils
{
    public static class ClassHelpers
    {
        public static bool IsTypeHasParentType<T>(Type type) where T : class
        {
            return typeof(T).IsAssignableFrom(type);
        }

        public static void CopyAllProperties<T, C>(T objectFrom, C objectTo)  where T : class where C : class
        {
            var properties = GetProperties(objectFrom);

            foreach (var property in properties)
            {
                var name = property.Name;
                var valueToCopy = GetPropValue(objectFrom, name);
                SetPropValue(objectTo, name, valueToCopy);
            }                       
        }
                
        private static PropertyInfo[] GetProperties(object src)
        {
            return src.GetType().GetProperties();
        }

        private static object GetPropValue(object src, string propName)
        {
            return src.GetType().GetProperty(propName).GetValue(src, null);
        }

        private static void SetPropValue(object src, string propName, object value)
        {
            src.GetType().GetProperty(propName).SetValue(src, value);
        }
    }
}
